package com.kotlin.android.router

import android.app.Activity
import android.os.Bundle
import android.os.Parcelable
import android.util.ArrayMap
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.kotlin.android.router.ext.put
import java.io.Serializable

/**
 * 创建者: zl
 * 创建时间: 2020/6/4 4:58 PM
 * 描述:页面跳转管理？是否需要这个类，有待商榷 todo
 */
object RouterManager {

    /**
     * 无参路由请求
     */
    fun navigation(path: String?) {
        path?.apply {
            ARouter.getInstance().build(this).navigation()
        }
    }

    /**
     * 有参路由请求
     */
    fun navigation(path: String?, bundle: Bundle?) {
        path?.apply {
            ARouter.getInstance().build(this).with(bundle).navigation()
        }
    }

    /**
     * 有参路由请求
     * vararg extras: 可变参数
     */
    /*fun navigation(path: String?, vararg extras: Pair<String, Any>) {
        path?.apply {
            val build = ARouter.getInstance().build(this)
            extras.forEach { (key, value) ->
                when (value) {
                    is Int -> build.withInt(key, value)
                    is Long -> build.withLong(key, value)
                    is Float -> build.withFloat(key, value)
                    is String -> build.withString(key, value)
                    is Short -> build.withShort(key, value)
                    is Boolean -> build.withBoolean(key, value)
                    is Double -> build.withDouble(key, value)
                    is Parcelable -> build.withParcelable(key, value)
                    is Serializable -> build.withSerializable(key, value)
                }
                //todo 根据实际业务再进行扩展
                build.navigation()
                //ARouter.getInstance().build(this).with(bundle).navigation()
            }
        }
    }*/


    /**
     * 有参路由请求
     */
    fun navigation(path: String?, map: ArrayMap<String, Any> = ArrayMap()) {
        path?.apply {
            ARouter.getInstance().build(this).with(Bundle().put(map)).navigation()
        }
    }

    /**
     * startActivityForResult
     * path 路径
     * activity 持有的当前上下文
     * map组装的参数，用Bundle进行传递
     * requestCode 请求码
     */
    fun navigation(
        path: String?,
        activity: Activity,
        map: ArrayMap<String, Any> = ArrayMap(),
        requestCode: Int
    ) {
        path?.apply {
            ARouter.getInstance().build(this).with(Bundle().put(map))
                .navigation(activity, requestCode)
        }
    }

    fun navigation(
        path: String?,
        activity: Activity,
        map: ArrayMap<String, Any> = ArrayMap(),
        callback: NavigationCallback
    ) {
        path?.apply {
            ARouter.getInstance().build(this).with(Bundle().put(map))
                .navigation(activity, callback)
        }
    }
}